<?php

namespace app\admin\model\user;

use think\Model;


class User extends Model
{

    

    //数据库
    protected $connection = 'database';
    // 表名
    protected $name = 'user';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'work_text',
        'level_text',
        'state_text',
        'vip_text',
        'vipetime_text',
        'prevtime_text',
        'logintime_text',
        'jointime_text'
    ];
    

    
    public function getTypeList()
    {
        return ['1' => __('Type 1'), '2' => __('Type 2')];
    }

    public function getWorkList()
    {
        return ['1' => __('Work 1'), '2' => __('Work 2'), '3' => __('Work 3'), '4' => __('Work 4')];
    }

    public function getLevelList()
    {
        return ['1' => __('Level 1'), '2' => __('Level 2'), '3' => __('Level 3'), '4' => __('Level 4')];
    }

    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2')];
    }

    public function getVipList()
    {
        return ['1' => __('Vip 1'), '2' => __('Vip 2')];
    }


    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getWorkTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['work']) ? $data['work'] : '');
        $list = $this->getWorkList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getLevelTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['level']) ? $data['level'] : '');
        $list = $this->getLevelList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVipTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['vip']) ? $data['vip'] : '');
        $list = $this->getVipList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getVipetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['vipetime']) ? $data['vipetime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getPrevtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['prevtime']) ? $data['prevtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getLogintimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['logintime']) ? $data['logintime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getJointimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['jointime']) ? $data['jointime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setVipetimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setPrevtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setLogintimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setJointimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }


    public function usergroup()
    {
        return $this->belongsTo('app\admin\model\UserGroup', 'group_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
